package com.bmoblistview;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity implements IOnChangeListener {
    private static final String TAG = "MainActivity";
    private ListView listView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initAdapter();
        QueryStudents();
        addStudent();
        EditStudent();
    }

    private void EditStudent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Student student = studentArrayList.get(position);
                Intent intent = new Intent(MainActivity.this, EditActivity.class);
                intent.putExtra("OBJECTID", student.getObjectId());
                intent.putExtra("NAME", student.getName());
                intent.putExtra("AGE", student.getAge());
                intent.putExtra("GENDER", student.getGender());
                startActivityForResult(intent, 1002);
            }
        });
    }

    private void addStudent() {
        btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    private void QueryStudents() {
        BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
        studentBmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if (e == null) {
                    studentArrayList.clear();
                    studentArrayList.addAll(list);
                    adapter.notifyDataChanged(studentArrayList);
                } else {
                    Log.e("QueryStudents", e.getMessage());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (data == null) {
            return;
        }
        if (resultCode == 2001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            String gender = data.getStringExtra("GENDER");

            final Student student = new Student(name, age, gender);
            student.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e == null) {
                        toast("添加成功：" + student.getName());
                        QueryStudents();
                    } else {
                        toast("添加失败：" + e.getMessage());
                    }
                }
            });
        } else if (resultCode == 3001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            String gender = data.getStringExtra("GENDER");
            String objectId = data.getStringExtra("OBJECTID");

            final Student student = new Student(name, age, gender);
            student.update(objectId, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        toast("修改成功");
                        QueryStudents();
                    } else {
                        toast("修改失败：" + e.getMessage());
                    }
                }
            });
        }
    }

    private void toast(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    private void initAdapter() {
        listView = findViewById(R.id.my_listView);
        adapter = new StuAdapter(MainActivity.this, studentArrayList, MainActivity.this);
        listView.setAdapter(adapter);
    }

    @Override
    public void del(String id) {
        Student student = new Student();
        student.setObjectId(id);
        student.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    toast("删除成功");
                    QueryStudents();
                } else {
                    toast("删除失败" + e.getMessage());
                }
            }
        });
    }
}
